﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using Edu.Web.Security;
using ESoft.Core.Util.Helpers;

namespace Edu.Web.Biz
{
    public class BaseController : Controller
    {
        #region GetMapModel、GetMapModels、GetRequestFilterMap、GetRequestMap
        private Dictionary<string, object> _map;

        /// <summary>
        /// 用户信息
        /// </summary>
        protected User CurrentUser
        {
            get
            {
                return (Request.RequestContext.HttpContext.User as FormsPrincipal<User>).UserData;
            }
        }

        public virtual T GetRequestMap<T>(string queryName, T defaultValue)
        {
            this._map = RequestMapHelper.GetRequestMap(this._map, Request.InputStream);
            return RequestMapHelper.GetRequestMap<T>(this._map, queryName, defaultValue);
        }

        public virtual Dictionary<string, object> GetRequestMap()
        {
            return RequestMapHelper.GetRequestMap(this._map, Request.InputStream);
        }

        public virtual T GetRequestModel<T>() where T : class, new()
        {
            this._map = RequestMapHelper.GetRequestMap(this._map, Request.InputStream);
            return RequestMapHelper.GetMapModel<T>(this._map);
        }
        #endregion

        protected override JsonResult Json(object data, string contentType,
            Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            if (behavior == JsonRequestBehavior.DenyGet
                && string.Equals(this.Request.HttpMethod, "GET",
                    StringComparison.OrdinalIgnoreCase))
                //Call JsonResult to throw the same exception as JsonResult
                return new JsonResult();
            return new JsonNetResult()
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
        }
    }
}